import 'package:e_book/model/activity.dart';
import 'package:e_book/model/book.dart';
import 'package:e_book/components/book_tile/my_book_tile.dart';
import 'package:e_book/components/my_search_tile.dart';
import 'package:e_book/pages/book_detail/book_detail_page.dart';
import 'package:e_book/pages/home/components/my_book_activities.dart';
import 'package:e_book/pages/home/components/my_book_activities_skeleton.dart';
import 'package:e_book/pages/home/components/my_book_activity_labels.dart';
import 'package:e_book/pages/home/components/my_book_activity_labels_skeleton.dart';
import 'package:e_book/pages/home/home_vm.dart';
import 'package:e_book/utils/navigator_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Map<String, dynamic>> books = [
    {
      'title': '食南之徒',
      'authorName': '马伯庸',
      'cover': 'https://img3.doubanio.com/view/subject/s/public/s34823157.jpg',
    },
    {
      'title': '早安，怪物',
      'authorName': '凯瑟琳·吉尔迪纳',
      'cover': 'https://img3.doubanio.com/view/subject/s/public/s34798823.jpg',
    },
    {
      'title': '十八岁出门远行',
      'authorName': '余华',
      'cover': 'https://img1.doubanio.com/view/subject/s/public/s34804300.jpg',
    },
    {
      'title': '三国前夜：士大夫政治与东汉皇权的崩解',
      'authorName': '张向荣',
      'cover': 'https://img3.doubanio.com/view/subject/s/public/s34823157.jpg',
    },
    {
      'title': '比山更高：自由攀登者的悲情与荣耀',
      'authorName': '宋明蔚',
      'cover': 'https://img3.doubanio.com/view/subject/s/public/s34887762.jpg',
    },
  ];

  final HomeViewModel _viewModel = HomeViewModel();

  @override
  void initState() {
    super.initState();
    _viewModel.getHomePageData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.surface,
      body: _getBodyUI(),
      // body: Text('666'),
    );
  }

  Widget _getBodyUI() {
    return ChangeNotifierProvider.value(
        value: _viewModel,
        builder: (context, child) {
          return SafeArea(
              child: SingleChildScrollView(
            padding: EdgeInsets.all(15.r),
            child: Column(
              children: [
                // 头像
                10.verticalSpace,
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '您好,饶又欠',
                      style: TextStyle(
                          fontSize: 22.sp, fontWeight: FontWeight.w600),
                    ),

                    // 头像
                    const CircleAvatar(
                      backgroundImage: AssetImage('images/avatar.jpeg'),
                    )
                  ],
                ),

                15.verticalSpace,

                // 搜索
                MySearchTitle(bookshelfTap: () {}),

                15.verticalSpace,

                // 读书活动

                Selector<HomeViewModel, List<Activity>?>(
                    builder: (context, List<Activity>? activities, child) {
                      if (activities == null) {
                        // 这里可以展示骨架
                        return const MyBookActivitiesSkeleton();
                      }
                      return MyBookActivities(activities: activities);
                    },
                    selector: (_, viewModel) => viewModel.activities),

                15.verticalSpace,

                // // 活动类型

                Selector<HomeViewModel, List<String>?>(
                    builder: (context, List<String>? labels, child) {
                      if (labels == null) {
                        // 这里可以展示骨架
                        return const MyBookActivityLabelsSkeleton();
                      }
                      return MyBookActivityLabels(labels: labels);
                    },
                    selector: (_, viewModel) => viewModel.activiLabels),

                15.verticalSpace,

                // // 特别为您准备

                Selector<HomeViewModel, List<Book>?>(
                  builder: (context, List<Book>? books, child) {
                    return MyBookTitle(
                      title: '特别为您准备',
                      books: books,
                      width: 120.w,
                      height: 160.h,
                      itemTap: (book) {
                        // 跳转到图书详情页
                        NavigatorUtils.nav2BookDetailPage(context, book: book);
                      },
                    );
                  },
                  selector: (_, viewModel) => viewModel.books,
                ),
              ],
            ),
          ));
        });
  }
}
